Birmingham | ITP-Jan | Roman Sanaye | Sprint 2 | Exercises#1019
Birmingham | ITP-Jan | Roman Sanaye | Sprint 2 | Exercises#1019RomanSanaye wants to merge 11 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-2/implement/lookup.js
Outdated
| let output = {}; | ||
| for (let pair of input) { | ||
| let key = pair[0]; | ||
| let value = pair[1]; | ||
| output[key] = value; |
There was a problem hiding this comment.
output and pair are not going to be reassigned a value, so a better practice is to declare them using const.
We can still modify the array or object assigned to a constant in JS, we just cannot reassign another value to the constant.
There was a problem hiding this comment.
let was replaced with const, and it is a safer option.
| let object = [1, 2, 3]; | ||
| let key = "name"; | ||
| expect(contains(object, key)).toEqual(false); | ||
| }); |
There was a problem hiding this comment.
In practice, we prepare tests not just to check our own implementation, but also to guard against future modification.
Currently, this incorrectly implemented function could also pass this test:
function contains(object, key) {
if (object === null || typeof object !== "object") {
return false;
}
return Object.prototype.hasOwnProperty.call(object, key);
}
There was a problem hiding this comment.
Thanks for the feedback! I’ve added a test to ensure arrays are rejected even when the key exists, so incorrect implementations will now fail.
|
All good now. Well done. |
Learners, PR Template
Self checklist
Changelist
totalTillfunction.totalTillfunction.Questions
No questions at this time.